package b2.widget.fort
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.MatchRankingInfo;
	import b2.widget.tab.*;

	public class CampInfoPane extends TabController
	{
		public var campDetailTab:CampDetailTab;
		private var matchDetailTab:MatchDetailTab;
		private var fortressDialog:FortressDialog;
		
		public function CampInfoPane(fortressDialog:FortressDialog)
		{
			super();
			this.fortressDialog = fortressDialog;
			campDetailTab = new CampDetailTab(fortressDialog);
			matchDetailTab = new MatchDetailTab(fortressDialog);
			setTabTitlePos(10, 0);
			setTabPagePos(0, 15);
			refresh();
		}
		
		public function refresh():void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			var CampInfoTitle:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr6");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr4");
			//比赛战役
			if (campConfig.type == 2)
			{
				if (!this.contains(matchDetailTab))
				{
					this.removeAllTabs();
					addTab(CampInfoTitle, matchDetailTab.tabName, matchDetailTab, HighlightTitleBgr);
					var matchRankingTab:MatchRankingTab = new MatchRankingTab("周排行"); 
					addTab(CampInfoTitle, matchRankingTab.tabName, matchRankingTab, HighlightTitleBgr);
					matchRankingTab = new MatchRankingTab("总排行");
					addTab(CampInfoTitle, matchRankingTab.tabName, matchRankingTab, HighlightTitleBgr);
				}
				matchDetailTab.refresh();
				refreshMatchRanking(matchDetailTab.matchIndex);
			}
			else
			{
				if (!this.contains(campDetailTab))
				{
					this.removeAllTabs();
					addTab(CampInfoTitle, campDetailTab.tabName, campDetailTab, HighlightTitleBgr);
				}
				campDetailTab.refresh();
			}	
		}
		
		private function refreshMatchRanking(matchIndex:int):void
		{
			ServerHelper.getInstance().getMatchInfo(matchIndex);
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_MATCH_INFO, onGetMatchInfoComplete);
		}
		
		private function onGetMatchInfoComplete(event:ServerEvent):void
		{
			var matchRankingList:Array = event.userData as Array;
			for (var i:int = 0; i < 2; i++)
			{
				var matchRankings:Array = matchRankingList[i];
				if (i + 1 < this.tabs.length)
				{
					var tab:Tab = this.tabs[i + 1];
					tab.removeAll();
					for (var j:int = 0; j < matchRankings.length; j++)
					{
						var matchRankingInfo:MatchRankingInfo = matchRankings[j];
						var matchRankingItem:MatchRankingItem = new MatchRankingItem(matchRankingInfo);
						tab.addItem(matchRankingItem, matchRankingItem.width, matchRankingItem.height);
					}
				}
			}
		}
		
	}
}